*! version 1.2
* 2 Sep 2013
* NIDS 
* Merging Household questionnaires across waves

/*

NOTE TO USERS:

This is a many-to-one matching on the Wave 1, Wave 2 and Wave 3 household questionnaires:
This occurs because a Wave 1 or Wave 2 household can split into one or more Wave 3 households. 
Thus the dataset created by this do file represents all unique combinations of households 
(i.e. only reords unique on w1_hhid, w2_hhid and w3_hhid combinations were retained). 
*/

*=====================================================================================================================================
* GLOBALS FOR DATA FILES AND VERSION SUFFIXES

global W1Data "\\137.158.104.21\data\Panel Public Release 2014a\Wave 1\Anon"
global W1VerIN "W1_Anon_V5.2"
global W2Data "\\137.158.104.21\data\Panel Public Release 2014a\Wave 2\Anon"
global W2VerIN "W2_Anon_V2.2"
global W3Data "\\137.158.104.21\data\Panel Public Release 2014a\Wave 3\Anon"
global W3VerIN "W3_Anon_V1.2"

global DataOUT "C:\Users\01406074\Desktop"
global VersionOUT "merged"

global temp "C:\Users\01406074\Desktop"					// tempfile to hold all the working datasets, all working datasets will
																// be deleted from this folder at the completion of the do file.
																		
version 12.0																		// version of Stata being used, this is needed for the rename command.


*=====================================================================================================================================

* OPENING LINK FILE, ADJUSTING FOR MERGE LATER ON

use "$W3Data\Link_File_$W3VerIN.dta", clear						// opening the link file
keep w1_hhid w2_hhid  w3_hhid  ///								// keeping only relevant variables
w1_hh_outcome w2_hh_outcome w3_hh_outcome //pid	     

*-------------------------------------------------------------------------------------------------------------------------------------

* OPENING THE WAVE 1 Household Questionnaire


merge m:1 w1_hhid using "$W1Data\HHQuestionnaire_$W1VerIN.dta", gen(w1_link)  		 // merging into adjusted link file from above
	

merge m:1 w2_hhid using "$W2Data\HHQuestionnaire_$W2VerIN.dta", gen(w2_link)	 // merging in the W2 Household questionnaire
	


merge m:1 w3_hhid using "$W3Data\HHQuestionnaire_$W3VerIN.dta", gen(w3_link)    // merging in the W3 Household questionnaire
drop if w1_link == 1															// dropping W1 households that merged into one household in W2/3
drop if w2_link == 1															// dropping W2 households that merged into one household in w3
drop if w3_link == 1


order w*_hhid w*_hh_outcome 													// ordering variables

duplicates drop w3_hhid w2_hhid w1_hhid, force									// keeping only unique W3 hhid's

drop w*_link																	// dropping variable created during the merges

save "$DataOUT\W1_W2_W2_HHQ_$VersionOUT.dta", replace							// saving out generated dataset


* end of do file

*======================================================================================================================================

